package homework1;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ZidongServlet")
public class ZidongServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Cookie[] cookies = request.getCookies();
		boolean iszi=false;
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("zidong")) {
				if(cookie.getValue().equals("true"))
					iszi=true;
			}
		}
		String name=null;
		String pwd=null;
		if(iszi) {
			for (Cookie cookie : cookies) {
				if(cookie.getName().equals("name"))
					name=cookie.getValue();
				if(cookie.getName().equals("pwd"))
					pwd=cookie.getValue();
			}
			if(LoginTest.Login(name, pwd)) {
				request.getRequestDispatcher("index.html").forward(request, response);
			}else
				request.getRequestDispatcher("login2.html").forward(request, response);
		}else
			request.getRequestDispatcher("login2.html").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
